package com.fafa.swing.base;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: TextFileOpener
 * @date Date : 2019年11月26日 22:58
 */
public class TextFileOpener extends JFrame {

    private JTextArea ta_showText;  //定义显示文件属性的文本域
    private JTextArea ta_showProperty;  //定义显示文件内容的文本域
    public static void main(String[] args) {
        TextFileOpener frame = new TextFileOpener();
        frame.setVisible(true);
    }

    public TextFileOpener(){
        setTitle("文本编辑器");
        setBounds(500,500,400,250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        final JMenu mn_file = new JMenu();
        mn_file.setText("文件");
        menuBar.add(mn_file);
        final JMenuItem mi_open = new JMenuItem();
        mi_open.addActionListener(new ActionListener() {
            //为打开菜单项添加监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                openTextFile();    //调用方法，操作文件
            }
        });
        mi_open.setText("打开");
        mn_file.add(mi_open);
        mn_file.addSeparator();
        final JMenuItem mi_exit = new JMenuItem();
        mi_exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        mi_exit.setText("退出");
        mn_file.add(mi_exit);
        final JMenu mn_edit = new JMenu();
        mn_edit.setText("编辑");
        menuBar.add(mn_edit);
        final JMenuItem mi_copy = new JMenuItem();
        mi_copy.setText("复制");
        mn_edit.add(mi_copy);
        final JMenuItem mi_cut = new JMenuItem();
        mi_cut.setText("剪切");
        mn_edit.add(mi_cut);
        final JMenuItem mi_paste = new JMenuItem();
        mi_paste.setText("粘贴");
        mn_edit.add(mi_paste);
        final JToolBar toolBar = new JToolBar();
        getContentPane().add(toolBar, BorderLayout.NORTH);
        final JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openTextFile();
            }
        });
        button.setText("打 开");
        toolBar.add(button);
        final JButton btn_exit = new JButton();
        btn_exit.setText("退 出");
        toolBar.add(btn_exit);
        final JTabbedPane tabbedPane = new JTabbedPane();
        getContentPane().add(tabbedPane,BorderLayout.CENTER);
        final JScrollPane scrollPane = new JScrollPane();

        tabbedPane.addTab("文件属性", null, scrollPane, null);
        ta_showProperty = new JTextArea();
        scrollPane.setViewportView(ta_showProperty);

        final JScrollPane scrollPane2 = new JScrollPane();
        tabbedPane.addTab("文件内容",null,scrollPane2, null);
        ta_showText = new JTextArea();
        scrollPane2.setViewportView(ta_showText);
    }

    public void openTextFile(){
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件","txt"));
        int returnVal = fileChooser.showOpenDialog(getContentPane());
        if(returnVal == JFileChooser.APPROVE_OPTION){
            //判断用户是否选择文件
            File file = fileChooser.getSelectedFile();
            ta_showProperty.append("文件的绝对路径："+file.getAbsolutePath()+"\n");
            ta_showProperty.append("文件是隐藏文件吗："+file.isHidden()+"\n");
            FileReader reader;
            BufferedReader in;
            try {
                reader = new FileReader(file);
                in = new BufferedReader(reader);
                String info = in.readLine();
                while (info != null){
                    ta_showText.append(info +"\n");
                    info = in.readLine();
                }
                in.close();
                reader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
